﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BySoft.Server.Business;
using BySoft.Server.Common.Entity;

namespace BySoft.Web.Site
{
    public class ExceptionModule : IHttpModule
    {
        #region IHttpModule Members
        public void Dispose()
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
            context.Error += new EventHandler(Application_Error);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //string str = HttpContext.Current.Session.SessionID;

        }

        void context_AcquireRequestState(object sender, EventArgs e)
        {

            //string str = HttpContext.Current.Session.SessionID;
        }

        #endregion


        private bool IsASPXPage(string url, ref string pageName)
        {
            bool blReturn = false;
            int nIndex = url.LastIndexOf(".");
            if (nIndex >= 0)
            {
                string strTemp = url.Substring(nIndex + 1);
                if (strTemp.Length >= 4)
                {
                    if (strTemp.Substring(0, 4).ToLower() == "aspx"
                        || strTemp.Substring(0, 4).ToLower() == "asmx")
                    {
                        blReturn = true;
                    }
                }
            }
            //if (blReturn)
            //{
            //    nIndex = url.LastIndexOf("/");
            //    string strTemp = string.Empty;
            //    if (nIndex > 0)
            //    {
            //        pageName = url.Substring(nIndex + 1);
            //        strTemp = url.Substring(0, nIndex);
            //        nIndex = strTemp.LastIndexOf("/");
            //        if (nIndex > 0)
            //        {
            //            pageName = strTemp.Substring(nIndex + 1) + "/" + pageName;
            //        }
            //    }
            //}
            pageName = url;
            return blReturn;
        }
        protected void Application_Error(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            string pageName = string.Empty;
            if (IsASPXPage(app.Request.Url.AbsolutePath, ref pageName))
            {
                try
                {
                    InsertExceptionLog(app, pageName);
                }
                catch
                {

                }
                HttpContext.Current.Response.Redirect("/ErrorPage.aspx");

            }

        }



        private void InsertExceptionLog(HttpApplication app, string pageName)
        {
            SysExceptonlogEntity enLog = new SysExceptonlogEntity();
            Exception ex = app.Server.GetLastError().InnerException;
            if (ex == null)
            {
                ex = app.Server.GetLastError();
            }
            enLog.ExceptionMessage = ex.Message;
            enLog.OrgStackTrace = ex.StackTrace;
            enLog.ExceptionUrl = app.Request.Url.ToString();
            enLog.IPAddress = app.Request.UserHostAddress;


            if (HttpContext.Current != null
                && HttpContext.Current.Session != null
                && HttpContext.Current.Session["Login_User"] != null)
            {
                SysUserEntity enUser = (SysUserEntity)HttpContext.Current.Session["Login_User"];
                enLog.UserID = enUser.UserID;
                enLog.UserName = enUser.UserName;
            }
            enLog.RecordTime = DateTime.Now;

            SysModuleEntity enModule = SysModule.GetLeftModuleInfoByUrl(pageName);
            enLog.ModuleID = enModule.ModuleID;
            enLog.ModuleName = enModule.ModuleName;
            enLog.ExceptionType = ex.GetType().ToString();

            SysExceptonlog.InsertSysExceptonlog(enLog);
        }
    }
}
